<template>
  <div class="gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.minioUploadConfig') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit(dataFormRef)"
    >
      <el-form-item
        label="minio—accessKey:"
        style="width:640px"
        prop="accessKeyId"
      >
        <el-input
          v-model="dataForm.accessKey"
          :placeholder="`minio ${$t('sysManagement.fileUpload')} accessKey`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        label="minio—secretKey:"
        style="width:640px"
        prop="accessKeySecret"
      >
        <el-input
          v-model="dataForm.secretKey"
          :placeholder="`minio ${$t('sysManagement.fileUpload')} secretKey`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        label="minio—endpoint:"
        style="width:640px"
        prop="endpoint"
      >
        <el-input
          v-model="dataForm.endpoint"
          :placeholder="`minio ${$t('sysManagement.fileUpload')} endpoint`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        label="minio—bucketName:"
        style="width:640px"
        prop="bucketName"
      >
        <el-input
          v-model="dataForm.bucketName"
          :placeholder="`minio ${$t('sysManagement.fileUpload')} bucketName`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.isActive')"
        style="width:640px"
        prop="isOpen"
      >
        <el-switch
          v-model="dataForm.isOpen"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <div
        class="default-btn"
        @click="onSubmit(dataFormRef)"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const emit = defineEmits(['activationConfig'])
const dataFormRef = ref(null)
const props = defineProps({
  saveUrl: {
    type: String,
    default: '/sys/pconfig/save'
  },
  changeFlag: {
    type: Boolean,
    default: false
  }
})

const dataForm = ref({
  accessKey: '',
  secretKey: '',
  endpoint: '',
  bucketName: '',
  isOpen: false
})
const dataRule = {}
const activationEventOriginate = ref(false)

watch(() => props.changeFlag, () => {
  if (!activationEventOriginate.value) {
    dataForm.value.isOpen = false
  }
  activationEventOriginate.value = false
})

onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'MINIO_OSS_CONFIG'),
      method: 'get',
      params: http.adornParams({
        id: dataForm.value.id,
        key: dataForm.value.paramKey
      })
    }).then(({ data }) => {
      if (data) {
        dataForm.value = JSON.parse(data)
      }
    })
    dataFormRef.value?.resetFields()
  })
}
const message = (msg, type) => {
  ElMessage({
    message: msg,
    type,
    duration: 1500
  })
}
const checkConfig = () => {
  if (dataForm.value.accessKey == null || dataForm.value.accessKey === '') {
    message(`minio ${$t('sysManagement.fileUpload')} accessKey ${$t('sysManagement.nullTips')} `, 'error')
    return false
  }
  if (dataForm.value.secretKey == null || dataForm.value.secretKey === '') {
    message(`minio ${$t('sysManagement.fileUpload')} secretKey ${$t('sysManagement.nullTips')} `, 'error')
    return false
  }
  if (dataForm.value.endpoint == null || dataForm.value.endpoint === '') {
    message(`minio ${$t('sysManagement.fileUpload')} endpoint ${$t('sysManagement.nullTips')} `, 'error')
    return false
  }
  if (dataForm.value.bucketName == null || dataForm.value.bucketName === '') {
    message(`minio ${$t('sysManagement.fileUpload')} bucketName ${$t('sysManagement.nullTips')} `, 'error')
    return false
  }
  return true
}
// 表单提交
const onSubmit = async formEl => {
  if (dataForm.value.isOpen && !checkConfig()) {
    return
  }

  if (!formEl) return
  await formEl.validate(valid => {
    if (!valid) return
    http({
      url: http.adornUrl(props.saveUrl),
      method: 'post',
      data: http.adornData({
        paramKey: 'MINIO_OSS_CONFIG',
        paramValue: JSON.stringify(dataForm.value),
        remark: $t('sysManagement.minioUploadConfig')
      })
    }).then(({ data }) => {
      if (data) {
        init()
      }
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          if (dataForm.value.isOpen) {
            activationEventOriginate.value = true
            emit('activationConfig')
          }
        }
      })
    })
  })
}

</script>

<style lang="scss" scoped>
@use '../index.scss';
</style>
